define(['jquery', 'xcConfirm'], function($, xcConfirm) {
    return {
        init: function(member) {
            $('body').keydown(function(e) {
                if (e.keyCode == 13) {
                    $("#loginBtn").click();
                    return false;
                }
            });
            //验证数据合法性
            $("#loginBtn").click(function() {
                //
                var account = $.trim($("#account").val());
                var password = $.trim($("#password").val());
                var _token  = $.trim($("#_token").val());
                var redirect_url = $.trim($("#redirect_url").val());
                var check_login_url = $.trim($("#check_login_url").val());
                if (account == "") {
                    var txt = "登录账号不能为空！";
                    var option = {
                        title: "登录提示",
                        onOk: function() {
                            $("#account").focus();
                        }
                    };
                    window.wxc.xcConfirm(txt, "custom", option);
                    return false;
                }
                //
                if (password == "") {
                    var txt = "登录密码不能为空！";
                    var option = {
                        title: "登录提示",
                        onOk: function() {
                            $("#password").focus();
                        }
                    };
                    window.wxc.xcConfirm(txt, "custom", option);
                    return false;
                }
                if (_token == "") {
                    var txt = "Token不能为空！";
                    var option = {
                        title: "登录提示",
                        onOk: function() {
                            
                        }
                    };
                    window.wxc.xcConfirm(txt, "custom", option);
                    return false;
                }
                //
                // var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;
                // if (!myreg.test(account)) {
                //     var txt = "请输入合法的手机号码！";
                //     var option = {
                //         title: "登录提示",
                //         onOk: function() {}
                //     }
                //     window.wxc.xcConfirm(txt, "custom", option);
                //     return false;
                // }
                //
                var htmlObj = $.ajax({
                    url: check_login_url,
                    type: 'POST',
                    data: {
                        "account": account,
                        "password": password,
                        "_token":_token,
                        "redirect_url": redirect_url,
                    },
                    async: false
                });
                var result = $.parseJSON(htmlObj.responseText);
                if (result['status'] != 0) {
                    var txt = result["msg"];
                    var option = {
                        title: "登录提示",
                        onOk: function() {}
                    };
                    window.wxc.xcConfirm(txt, "custom", option);
                    return false;
                }
                //重定向
                window.location.href = result['redirect_uri'];
            })
        }
    }
});